<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){

                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";   

                if(!empty( $keyword ))
                {
                    //用 户 名 :  $user
                    //密　　码 :  $pwd
                    //主库域名 :  $host
                    //从库域名 :  SAE_MYSQL_HOST_S
                    //端　　口 :  $port
                    //数据库名 :  $dbname

                    $dbname = "bdm255853512_db";
                    $host   = "bdm255853512.my3w.com";
                    $port   = "3306";
                    $user   = "bdm255853512";
                    $pwd    = "qf666999";

                    /*接着调用mysql_connect()连接服务器*/
                    // 连主库
                    $db = mysql_connect($host,$user,$pwd);

                    if(!$db){
                        die("Connect Server Failed: " . mysql_error($db));
                    }

                    /*连接成功后立即调用mysql_select_db()选中需要连接的数据库*/
                    if (!mysql_select_db($dbname)) {
                        die("Select Database Failed: " . mysql_error($db));
                    }


                    /*至此连接已完全建立，就可对当前数据库进行相应的操作了*/
                    /*！！！注意，无法再通过本次连接调用mysql_select_db来切换到其它数据库了！！！*/
                    /* 需要再连接其它数据库，请再使用mysql_connect+mysql_select_db启动另一个连接*/

                    /**
                     * 接下来就可以使用其它标准php mysql函数操作进行数据库操作
                     */
                     mysql_query("set names utf-8",$db); 
                     $sql="SELECT * FROM  `qf_choujiang` WHERE 1 LIMIT 0 , 30";
                     $query=mysql_query($sql);
                     $rs=mysql_fetch_array($query);
                     $b= $rs['num'];
                    $msgType = "text";
                    $contentStr = "你是第{$b}个睡觉的！";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                  
                    echo $resultStr;
                    $b++;
                    $sql="UPDATE `qf_choujiang` SET `num`={$b} WHERE 1";
                    mysql_query($sql);
                    mysql_close($link);
                 }else{
                        echo "Input something...";
                 }


        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
